package cn.iocoder.an.module.business.service.reportconfig;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.module.business.dal.dataobject.reportviewnew.ReportConfigDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.an.module.business.controller.admin.reportconfig.vo.*;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.business.dal.mysql.reportconfig.ReportConfigMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.business.enums.ErrorCodeConstants.*;

/**
 * 报表配置 Service 实现类
 *
 * @author zzy
 */
@Service
@Validated
public class ReportConfigServiceImpl implements ReportConfigService {

    @Resource
    private ReportConfigMapper reportConfigMapper;

    @Override
    public Long createReportConfig(ReportConfigSaveReqVO createReqVO) {
        // 插入
        ReportConfigDO reportConfig = BeanUtils.toBean(createReqVO, ReportConfigDO.class);
        reportConfigMapper.insert(reportConfig);

        // 返回
        return reportConfig.getId();
    }

    @Override
    public void updateReportConfig(ReportConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateReportConfigExists(updateReqVO.getId());
        // 更新
        ReportConfigDO updateObj = BeanUtils.toBean(updateReqVO, ReportConfigDO.class);
        reportConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteReportConfig(Long id) {
        // 校验存在
        validateReportConfigExists(id);
        // 删除
        reportConfigMapper.deleteById(id);
    }

    @Override
        public void deleteReportConfigListByIds(List<Long> ids) {
        // 删除
        reportConfigMapper.deleteByIds(ids);
        }


    private void validateReportConfigExists(Long id) {
        if (reportConfigMapper.selectById(id) == null) {
            throw exception(REPORT_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public ReportConfigDO getReportConfig(Long id) {
        return reportConfigMapper.selectById(id);
    }

    @Override
    public PageResult<ReportConfigDO> getReportConfigPage(ReportConfigPageReqVO pageReqVO) {
        return reportConfigMapper.selectPage(pageReqVO);
    }

}